home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / dte5_1.zip / UTILS.C < prev    next >
C/C++ Source or Header  |  1991-02-06  |  56KB  |  1,884 lines

  1. /*
  2.  * Written by Douglas Thomson (1989/1990)
  3.  *
  4.  * This source code is released into the public domain.
  5.  */
  6.  
  7. /*
  8.  * Name:    dte - Doug's Text Editor program - miscellaneous utilities
  9.  * Purpose: This file contains miscellaneous functions that were required
  10.  *           in more than one of the other files, or were thought to be
  11.  *           likely to be used elsewhere in the future.
  12.  * File:    utils.c
  13.  * Author:  Douglas Thomson
  14.  * System:  this file is intended to be system-independent
  15.  * Date:    October 1, 1989
  16.  */
  17.  
  18. #ifdef HPXL
  19. #include "commonh"
  20. #include "utilsh"
  21. #else
  22. #include "common.h"
  23. #include "utils.h"
  24. #endif
  25. #include <time.h>
  26. #ifdef __TURBOC__
  27. #include <dir.h>        /* for making temporary file names etc */
  28. #endif
  29.  
  30. /*
  31.  * prototypes for all functions in this file
  32.  */
  33. int myisalnum ARGS((char c));
  34. int linelen ARGS((text_ptr s));
  35. int prelinelen ARGS((text_ptr s));
  36. text_ptr find_next ARGS((text_ptr s));
  37. text_ptr find_prev ARGS((text_ptr current));
  38. void copy_line ARGS((windows *window));
  39. void un_copy_line ARGS((windows *window));
  40. int expand ARGS((text_ptr dest, text_ptr end));
  41. int load_file ARGS((char *name, int fixup));
  42. void set_prompt ARGS((char *prompt, int lines));
  43. int get_name ARGS((char *prompt, int lines, char *name));
  44. void fix_marks ARGS((windows *window, text_ptr pos, long len));
  45. int get_ynaq ARGS((windows *window));
  46. int get_yn ARGS((windows *window));
  47. int get_oa ARGS((windows *window));
  48. char get_attr ARGS((windows *window, text_ptr text));
  49. int update_line ARGS((windows *window, text_ptr orig, int line,
  50.         text_ptr cursor));
  51. int display_window ARGS((windows *window, int last, text_ptr cursor, int wn));
  52. int display ARGS((do_func doit, int reserved));
  53. void setup_window ARGS((windows *window));
  54. int first_non_blank ARGS((char *s));
  55. void page_up ARGS((windows *window));
  56. void page_down ARGS((windows *window));
  57. void scroll_down ARGS((windows *window));
  58. void scroll_up ARGS((windows *window));
  59. void save_file ARGS((windows *window, int kind));
  60. void save_as_file ARGS((windows *window));
  61.  
  62. /*
  63.  * Name:    myisalnum
  64.  * Purpose: To determine whether or not a character is part of a "word",
  65.  *           which in languages like Pascal means a letter, digit or
  66.  *           underscore.
  67.  * Date:    October 1, 1989
  68.  * Passed:  c: the character to be tested
  69.  * Returns: TRUE if c is an alphanumeric or '_' character, FALSE otherwise
  70.  */
  71. int myisalnum(c)
  72. char c;
  73. {
  74.     if ((c >= 'A' && c <= 'Z') ||
  75.             (c >= 'a' && c <= 'z') ||
  76.             (c >= '0' && c <= '9') ||
  77.             (c == '_')) {
  78.         return TRUE;
  79.     }
  80.     return FALSE;
  81. }
  82.  
  83. /*
  84.  * Name:    linelen
  85.  * Purpose: To determine the length of a line, up to either a \n or a
  86.  *           \0, whichever comes first.
  87.  * Date:    October 1, 1989
  88.  * Passed:  s: the line to be measured
  89.  * Returns: the length of the line
  90.  */
  91. int linelen(s)
  92. text_ptr s;
  93. {
  94.     int len = 0;
  95.  
  96.     while (*s && *s != '\n') {
  97.         ++len;
  98.         ++s;
  99.     }
  100.     return len;
  101. }
  102.  
  103. /*
  104.  * Name:    prelinelen
  105.  * Purpose: To determine the length of a line, from the current position
  106.  *           backwards to either a \n or a \0, whichever comes first.
  107.  * Date:    October 1, 1989
  108.  * Passed:  s: the line to be measured
  109.  * Returns: the length of the line up to the current position
  110.  * Notes:   It is assumed there will be a "terminating" \0 before the
  111.  *           start of the first line. This is the case with the main
  112.  *           text buffer, but elsewhere beware.
  113.  */
  114. int prelinelen(s)
  115. text_ptr s;
  116. {
  117.     int len = 0;
  118.  
  119.     while (*--s && *s != '\n') {
  120.         ++len;
  121.     }
  122.     return len;
  123. }
  124.  
  125. /*
  126.  * Name:    find_next
  127.  * Purpose: To find the first character in the next line after the starting
  128.  *           point.
  129.  * Date:    October 1, 1989
  130.  * Passed:  s: the starting point
  131.  * Returns: the first character in the next line
  132.  */
  133. text_ptr find_next(s)
  134. text_ptr s;
  135. {
  136.     while (*s && *s != '\n') {
  137.         ++s;
  138.     }
  139.     if (*s) {
  140.         return ++s;
  141.     }
  142.     return NULL;
  143. }
  144.  
  145. /*
  146.  * Name:    find_prev
  147.  * Purpose: To find the start of the line before the current line.
  148.  * Date:    October 1, 1989
  149.  * Passed:  current: the current line
  150.  * Returns: the start if the previous line
  151.  * Notes:   current must be at the start of the current line to begin with.
  152.  *          There must be a \0 preceding the first line.
  153.  */
  154. text_ptr find_prev(current)
  155. text_ptr current;
  156. {
  157.     if (*--current == '\0') {
  158.         return NULL;
  159.     }
  160.     for (;;) {
  161.         if (*--current == '\n' || *current == '\0') {
  162.             return ++current;
  163.         }
  164.     }
  165. }
  166.  
  167. /*
  168.  * Name:    copy_line
  169.  * Purpose: To copy the cursor line, if necessary, into the current line
  170.  *           buffer, so that changes can be made efficiently.
  171.  * Date:    October 1, 1989
  172.  * Passed:  window: access to the current line
  173.  * Notes:   As the cursor line is being copied, any markers that are set
  174.  *           within the line are also copied.
  175.  *          Trailing spaces left on the line (presumably from earlier
  176.  *           editing) are removed during the copy.
  177.  *          See un_copy_line, the reverse operation.
  178.  */
  179. void copy_line(window)
  180. windows *window;
  181. {
  182.     text_ptr p, q;     /* destination and source of copy */
  183.     int count;         /* number of characters copied */
  184.     int i;             /* for updating markers */
  185.     text_ptr end_line; /* end of line after removing trailing spaces */
  186.  
  187.     /*
  188.      * If the line has already been copied, then do not copy it again
  189.      */
  190.     if (g_status.copied) {
  191.         return;
  192.     }
  193.  
  194.     /*
  195.      * record that the current line buffer is active
  196.      */
  197.     g_status.copied = TRUE;
  198.  
  199.     /*
  200.      * clear any old buffer markers left from last time
  201.      */
  202.     for (i=0; i < NO_MARKS; i++) {
  203.         g_status.buff_marker[i] = NULL;
  204.     }
  205.  
  206.     /*
  207.      * find out where the line should end after removing trailing
  208.      *  spaces.
  209.      */
  210.     end_line = q = window->cursor;
  211.     while (*q && *q != '\n') {
  212.         if (*q++ != ' ') {
  213.             end_line = q;
  214.         }
  215.     }
  216.  
  217.     /*
  218.      * copy the cursor line to the line buffer, noting any markers
  219.      *  passed along the way
  220.      */
  221.     p = g_status.line_buff;
  222.     q = window->cursor;
  223.     for (count=0; ; ) {
  224.         for (i=0; i < NO_MARKS; i++) {
  225.             if (q == window->file_info->marker[i]) {
  226.                 g_status.buff_marker[i] = p;
  227.             }
  228.         }
  229.         if (*q == '\n') {
  230.             *p++ = *q++;
  231.             break;
  232.         }
  233.         if (*q == '\0') {
  234.             break;
  235.         }
  236.  
  237.         /*
  238.          * avoid copying trailing spaces
  239.          */
  240.         if (q < end_line) {
  241.             if (++count >= BUFF_SIZE) {
  242.                 error(WARNING, "line buffer overflow - line truncated!");
  243.                 break;
  244.             }
  245.             *p++ = *q;
  246.         }
  247.         ++q;
  248.     }
  249.     *p = '\0';
  250. }
  251.  
  252. /*
  253.  * Name:    un_copy_line
  254.  * Purpose: To copy the cursor line, if necessary, from the current line
  255.  *           buffer, shifting the main text to make the right amount of
  256.  *           room.
  257.  * Date:    October 1, 1989
  258.  * Passed:  window: access to the current line
  259.  * Notes:   As the cursor line is being copied, any markers that are set
  260.  *           within the line buffer are also copied.
  261.  *          For various reasons, trailing spaces are NOT removed when
  262.  *           returning the line buffer to the main text. Typically,
  263.  *           padding is added at the end of a line by deliberately
  264.  *           adding trailing spaces, and then uncopying the line.
  265.  *          See copy_line, the reverse operation.
  266.  */
  267. void un_copy_line(window)
  268. windows *window;
  269. {
  270.     text_ptr source; /* source for block move and for copying buffer line */
  271.     text_ptr dest;   /* destination for block move and copy */
  272.     long number;     /* length of block move */
  273.     int len;         /* length of current line buffer text */
  274.     int curs_len;    /* length of cursor line */
  275.     int i;           /* used for checking markers */
  276.  
  277.     /*
  278.      * do not uncopy unless the line buffer is active